package server.communication.ClientMessages;

import java.io.IOException;

import server.communication.Client;
import server.communication.MessageProcessor;
import server.communication.Server;

public class PingMessageThread extends ClientMessageThread{

	public PingMessageThread(Client parent) throws IOException
	{
		super (parent);
	}
	
	boolean state = false;
	
	public boolean getLastState()
	{
		return state;
	}
	
	@Override
	public void run()
	{
		while (true)
		{
			try
			{
				Thread.sleep(10000);
				if (parent.aliveMessageThreads() > 0)
					continue;
				dontPauseNow();
				out.println(MessageProcessor.pingQuery());
				String input = in.readLine();
				pauseOK();
				if (input != null)
				{
					state = true;
					if (!input.startsWith("ping:"))
						parent.getMessageProcessor().process(input);
					state = true;
				}
			}
			catch(Exception e)
			{
				System.out.println("User "+parent.getHostName()+" didn't answer for ping request\n\tConnection lost");
				break;
			}
		}
		parent.removeMessageThread(this);
		Server.getInstance().removeClient(parent);
	}
}
